package com.uniview.service.orderType.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.uniview.entity.vo.Pager;
import com.uniview.entity.vo.orderType.OnlineProblems;
import com.uniview.plugins.adapter.OLDataSource;
import com.uniview.service.orderType.OnlineProblemsService;

@Service("onlineProblemsService")
public class OnlineProblemsServiceImpl implements OnlineProblemsService {

	Logger logger = org.apache.log4j.Logger.getLogger(OnlineProblemsServiceImpl.class);

	@Value("#{configProperties['uniview.OnlineProblems']}")
	private String OnlineProblemsTable;
	
	@Autowired
	private OLDataSource olDataSource;

	@Override
	public Pager<OnlineProblems> findOnlineProblemsByWorkNo(String workNo,String queryString, int page, int limit) throws Exception {
		workNo = workNo.toUpperCase();
		StringBuilder builder = new StringBuilder();
		builder.append("select * from (select A.*,ROWNUM as id FROM ( ");
		builder.append("select * from " + OnlineProblemsTable + " where 1=1 ");
		if(StringUtils.isNotBlank(queryString)){
			builder.append(" and (title like '%"+queryString+"%' or company_name like '%"+queryString+"%')");
		}
		builder.append(" ) A WHERE ROWNUM <= " + page + " * " + limit + " ) WHERE id > (" + page + " - 1) *" + limit);
		logger.debug("========获取网上问题" + builder.toString());
		String countSql = "select count(*) from " + OnlineProblemsTable + " where 1=1 ";
		if(StringUtils.isNotBlank(queryString)){
			countSql += " and (title like '%"+queryString+"%' or company_name like '%"+queryString+"%')";
		}
		List<OnlineProblems> list = olDataSource.queryForList(OnlineProblems.class, builder.toString());
		int total = olDataSource.getCount(countSql);
		Pager<OnlineProblems> pager = new Pager<OnlineProblems>();
		pager.setPagerList(list);
		pager.setTotalRow(total);
		return pager;
	}

}
